package junit;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Queue;
import java.util.Random;

import org.junit.Test;

import com.coo8.common.queue.RedisQueueFactory;

public class QueueFactoryTest1 {
	
	private static String redis_ip = "10.57.4.108";
	private static String YYYYMMDD = new SimpleDateFormat("yyyyMMdd").format(new Date());
	//private static String redis_ip = "10.58.22.190";
	//private static String filePath = "D:/��˾��Ϣ/��Ŀ��Ϣ/����/�����˿/1Ԫ��ȯ/1/1/����/10_20130411.txt";
	private static String filePath = "D:\\Workspaces\\MyEclipse10WorkSpace\\ReidsTest\\src\\doc\\local_item_prelive.txt";
	/**
	 * �������
	 */
	public void clearRedis() {
		/*System.out.println("�������**************clearRedis");
		try{
	        RedisQueueFactory factory = new RedisQueueFactory();
			factory.setIp(redis_ip);
	        factory.setPort(6379);
	        factory.setTimeOut(5000);
	        factory.init();
			Queue<String> queue = factory.buildQueue("send_coupon_info", String.class);
			queue.clear();
			System.out.println(queue.size());
		}catch(Exception e){
			e.printStackTrace();
		}*/
	}
	
	
	
	/**
	 * �鿴�齫�������Ϣ
	 */
	@Test
	public void getRedisInfo(){
		System.out.println("�鿴�齫�������Ϣ**************getRedisInfo");
		try{
	        RedisQueueFactory factory = new RedisQueueFactory();
			factory.setIp(redis_ip);
	        factory.setPort(6379);
	        factory.setTimeOut(5000);
	        factory.init();
			//Queue<String> queue = factory.buildQueue("bound_coupon_14_20130417", String.class);
			Queue<String> queue = factory.buildQueue(YYYYMMDD+"_gome_luck_draw_promo130614", String.class);
			System.out.println("�齫����ʣ��������"+queue.size());
			System.out.println("��һ���齱���룺"+queue.peek());
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * ��ӳ齱���뵽Redis
	 */
	public void addLuckNoRedis() {
		 
		/*System.out.println("��ӳ齱���뵽Redis**************addLuckNoRedis");
		long ll = new Date().getTime();
		RedisQueueFactory factory = new RedisQueueFactory();
		factory.setIp(redis_ip);
	    factory.setPort(6379);
	    factory.setTimeOut(5000);
	    factory.init();
	    int[] a = {10,14,20,21};
	    int ii = 1;
	    for(int i=10;i<=17;i++){
	    	for(int k=0;k<a.length;k++){
			    String fname = a[k]+"_201304"+(i>=10?i:"0"+i);
			    System.out.println("��ʼ======================bound_coupon_"+fname);
				Queue<String> queue = factory.buildQueue("bound_coupon_"+fname, String.class);
				File file = new File(filePath+fname+".txt");
				InputStreamReader read;
				try {
					read = new InputStreamReader(new FileInputStream(file),"UTF-8");
					BufferedReader bufferedReader = new BufferedReader(read);  
					String lineTXT = null;  
					while ((lineTXT = bufferedReader.readLine()) != null) {
						if(lineTXT!=null&&!"".equals(lineTXT.toString().trim())){
							queue.add(lineTXT.toString().trim());
							System.out.println((ii++)+"��Ӻ���======"+lineTXT.toString().trim());
						}
					}
					read.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				System.out.println("����======================bound_coupon_"+fname);
	    	}
	    }
		System.out.println("��ӳ齱���뵽Redis============����");
		System.out.println("end="+(System.currentTimeMillis()-ll));*/
	}
	
	
	
	/**
	 * ��ӱ���ȯ��
	 */
	public void addLuckNoRedisBF() {
		 
		/*System.out.println("��ӳ齱���뵽Redis**************addLuckNoRedis");
		long ll = new Date().getTime();
		RedisQueueFactory factory = new RedisQueueFactory();
		factory.setIp(redis_ip);
	    factory.setPort(6379);
	    factory.setTimeOut(5000);
	    factory.init();
	   
		Queue<String> queue = factory.buildQueue("send_coupon_info", String.class);
		File file = new File(filePath);
		InputStreamReader read;
		try {
			read = new InputStreamReader(new FileInputStream(file),"UTF-8");
			BufferedReader bufferedReader = new BufferedReader(read);  
			String lineTXT = null;
			int i = 1;
			while ((lineTXT = bufferedReader.readLine()) != null) {
				if(lineTXT!=null&&!"".equals(lineTXT.toString().trim())){
					queue.add(lineTXT.toString().trim());
					System.out.println(lineTXT.toString().trim()+"=========="+(i++));
				}
			}
			read.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("��ӳ齱���뵽Redis============����");
		System.out.println("end="+(System.currentTimeMillis()-ll));*/
	}
	
	
	
	/**
	 * ���_�齱 ����
	 */
	@Test
	public void addLuckNoRedisCJ() {
		System.out.println("��ӳ齱���뵽Redis**************addLuckNoRedis");
		long ll = new Date().getTime();
		RedisQueueFactory factory = new RedisQueueFactory();
		factory.setIp(redis_ip);
	    factory.setPort(6379);
	    factory.setTimeOut(5000);
	    factory.init();
	    
	    List<String> list=new ArrayList<String>();
		Queue<String> queue = factory.buildQueue(YYYYMMDD+"_gome_luck_draw_promo130614", String.class);
		File file = new File(filePath);
		InputStreamReader read;
		try {
			read = new InputStreamReader(new FileInputStream(file),"UTF-8");
			BufferedReader bufferedReader = new BufferedReader(read);  
			String lineTXT = null;
			int i = 1;
			while ((lineTXT = bufferedReader.readLine()) != null) {
				if(lineTXT!=null&&!"".equals(lineTXT.toString().trim())){
					list.add(lineTXT.toString().trim());
					System.out.println(lineTXT.toString().trim()+"=========="+(i++));
				}
			}
			read.close();
			
			addNoToQueue(list, queue);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("��ӳ齱���뵽Redis============����");
		System.out.println("end="+(System.currentTimeMillis()-ll));
	}
	
	public static void addNoToQueue(List<String> list,Queue<String> queue){
		if (list!=null && list.size()>0) {
			for (int i = 0; i < list.size(); i++) {
				String str=list.get(i);
				for (int j = 0; j < 300; j++) {
					queue.add("N_N_��δ�н�_100");
				}
				queue.add(str);
			}
		}
	}
	
	
	/**
	 * ��ɺ����
	 */
	public void shengChangHaoMaChi() {
		System.out.println("��ɺ����**************shengChangHaoMaChi");
		long ll = new Date().getTime();
		System.out.println("set num="+ll);
		int[] seed =  new int[50000000];
		for(int j=0;j<50000000;j++){
			seed[j]=j;
		}
		int[] seed1 =  new int[50000000];
		int a = 0;
		for(int j=50000000;j<100000000;j++){
			seed1[a]=j;
			a++;
		}
		System.out.println("use num="+(new Date().getTime()-ll));
		
		int[] ranArr1 = new int[50000000];
		int[] ranArr2 = new int[50000000];
		Random ran = new Random();
		ll = System.currentTimeMillis();
		System.out.println("string="+ll);
		// ����������Լ����塣
		for (int i = 0; i<50000000;i++){
			// �õ�һ��λ��
			int j = ran.nextInt(seed.length-i);
			// �õ��Ǹ�λ�õ���ֵ
			ranArr1[i] = seed[j];
			// �����һ��δ�õ����ַŵ�����
			seed[j] = seed[seed.length-1-i];
		}
		
		for (int i = 0; i<50000000;i++){
			// �õ�һ��λ��
			int j = ran.nextInt(seed1.length-i);
			// �õ��Ǹ�λ�õ���ֵ
			ranArr2[i] = seed1[j];
			// �����һ��δ�õ����ַŵ�����
			seed1[j] = seed1[seed1.length-1-i];
		}
		
		try {
			PrintWriter writer0 = new PrintWriter(new File("D:\\��ɾ�����\\�����0.txt"));
			PrintWriter writer1 = new PrintWriter(new File("D:\\��ɾ�����\\�����1.txt"));
			PrintWriter writer2 = new PrintWriter(new File("D:\\��ɾ�����\\�����2.txt"));
			PrintWriter writer3 = new PrintWriter(new File("D:\\��ɾ�����\\�����3.txt"));
			PrintWriter writer4 = new PrintWriter(new File("D:\\��ɾ�����\\�����4.txt"));
			PrintWriter writer5 = new PrintWriter(new File("D:\\��ɾ�����\\�����5.txt"));
			PrintWriter writer6 = new PrintWriter(new File("D:\\��ɾ�����\\�����6.txt"));
			PrintWriter writer7 = new PrintWriter(new File("D:\\��ɾ�����\\�����7.txt"));
			PrintWriter writer8 = new PrintWriter(new File("D:\\��ɾ�����\\�����8.txt"));
			PrintWriter writer9 = new PrintWriter(new File("D:\\��ɾ�����\\�����9.txt"));
		int k1=0,k2=0,f=0;
		for(int ii=0;ii<100000000;ii++){
			String no ="";
			if(ii%2==0){
				no = this.redisCode(String.valueOf(ranArr1[k1]));
				k1++;
			}else{
				no = this.redisCode(String.valueOf(ranArr2[k2]));
				k2++;
			}
			int b = Integer.valueOf(ii/10000000) ;
			switch (b) {
			case 0:
				writer0.println(no);
				break;
			case 1:
				writer1.println(no);
				break;
			case 2:
				writer2.println(no);
				break;
			case 3:
				writer3.println(no);
				break;
			case 4:
				writer4.println(no);
				break;
			case 5:
				writer5.println(no);
				break;
			case 6:
				writer6.println(no);
				break;
			case 7:
				writer7.println(no);
				break;
			case 8:
				writer8.println(no);
				break;
			case 9:
				writer9.println(no);
				break;
			default:
				break;
			}
			if(f!=b){
				System.out.println("����±�="+b);
				switch (f) {
				case 0:
					writer0.flush();
					writer0.close();
					break;
				case 1:
					writer1.flush();
					writer1.close();
					break;
				case 2:
					writer2.flush();
					writer2.close();
					break;
				case 3:
					writer3.flush();
					writer3.close();
					break;
				case 4:
					writer4.flush();
					writer4.close();
					break;
				case 5:
					writer5.flush();
					writer5.close();
					break;
				case 6:
					writer6.flush();
					writer6.close();
					break;
				case 7:
					writer7.flush();
					writer7.close();
					break;
				case 8:
					writer8.flush();
					writer8.close();
					break;
				case 9:
					writer9.flush();
					writer9.close();
					break;
				default:
					break;
				}
				f=b;
			}
		}
		System.out.println("end="+(System.currentTimeMillis()-ll));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}  
		
	         
	}
	
	
	/*
	 * �ַ����λ�Ĳ���
	 */
	public String redisCode(String code){
		int ii=8-code.length();
		if(code!=null && !code.equals("")){
			if(code.length()<8){
				for(int i=0;i<ii;i++){
					code="0"+code;
				}
			}
		}
		return code;
	}


}
